package com.example.exploreactivedemo1.fragments

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.ActivityOptionsCompat
import androidx.navigation.ActivityNavigator
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import com.example.exploreactivedemo1.R
import com.example.exploreactivedemo1.base.BaseFragment
import com.example.exploreactivedemo1.databinding.FragmentLoginBinding
import androidx.core.util.Pair as UtilPair


class LoginFragment : BaseFragment<FragmentLoginBinding>(){

    /**
     * BaseFragment里onCreateView会执行创建binding对象的操作。但具体的inflateBinding的实现，
     * 各个子类是不一样的(例如FragmentLoginBinding对象,FragementForgetBinding对象)，
     * 因此这个父类弄个了inflateBinding的抽象方法，子类需要重写它，重写的目的，就是子类可以向父类提供自己的Binding对象
     * 例如LoginFragment里重写了inflateBinding的抽象方法，并返回一个FragmentLoginBinding对象。这是这次提交的核心
     */
    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentLoginBinding {
        return FragmentLoginBinding.inflate(inflater)
    }

    override fun initView(rootView: View?) {
        super.initView(rootView)

        fragementBinding.jumpToForgetPasswdPageBtn.setOnClickListener {
            Log.e("LoginFragment","jumpToForgetPasswdPage was clicked")
            //跳转到忘记密码页面
            findNavController().navigate(R.id.to_forget_fragment)
        }

        fragementBinding.jumpToRegisterBtnPageBtn.setOnClickListener {
            Log.e("LoginFragment","jumpToRegisterBtnPage was clicked")

            //*****步骤3： 正确将共享元素传递给导航，共享元素通过Pair<View, String>的方式，将imageView与特定的transitionName "userAvatarTransition"联系起来，通过LoginFragment导航到RegisterFragment*****
            //extras ，这是 Navigation Component 中设置共享元素的正确方式
            //创建 Pair<View, String> - 正确地将 View 和 transitionName 配对
            //transitionName 一致 - 确保源和目标 Fragment 都使用 "userAvatarTransition"
            //在 navigate() 中传递 extras - 正确地将共享元素信息传递给导航
            val imagePair1 : Pair<View, String> = Pair<View, String>(fragementBinding.userAvatarLoginImageView,"userAvatarTransition")
            //再增加一个过渡元素的Pair，用在输入框上
            val inputPair2 : Pair<View, String> = Pair<View, String>(fragementBinding.loginInput,"accountInputTransition")
            //extras使用多个pair
            val extras : FragmentNavigator.Extras = FragmentNavigatorExtras(imagePair1,inputPair2)

            //准备传递的数据
            val bundle = Bundle()
            bundle.putString("userName",fragementBinding.loginInput.text.toString())

            //跳转到注册页面
            findNavController().navigate(R.id.to_register_fragment, args = bundle, navOptions =  null, navigatorExtras = extras)
        }

        //跳转到隐私协议activity
        fragementBinding.jumpToAgreementActivityBtn.setOnClickListener { view ->
            Log.e("LoginFragment","jumpToAgreementActivityBtn was clicked")
//            val pair : Pair<View, String> = Pair<View, String>(fragementBinding.userAvatarLoginImageView,"userAvatarTransition")
            val pair = UtilPair(fragementBinding.userAvatarLoginImageView as View,"userAvatarTransition")
            //pair2即新增的帐号输入框的transition
            var pair2 = UtilPair(fragementBinding.loginInput as View, "accountInputTransition")
            val options = ActivityOptionsCompat.makeSceneTransitionAnimation(requireActivity(),pair,pair2)
            val extras = ActivityNavigator.Extras.Builder().setActivityOptions(options).build()

            //准备传递的数据,activity之间传递数据
            val bundle = Bundle()
            bundle.putString("userName",fragementBinding.loginInput.text.toString())

            findNavController().navigate(R.id.agreement_activity, args = bundle, navOptions = null,navigatorExtras = extras)
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_login
    }
}